<html>
<head>
    <title>双预防系统</title>
    <link rel="stylesheet" href="../../../assets/bundle.element.css">
    <link rel="stylesheet" href="../../../style.css">

    <style>
        body {
            background: #F0F2F6;
        }

        .page-main {
            margin-bottom: 0px;
        }

        .main-head {
            background: #fff;
            margin-bottom: 0px;
        }

        .chart-outer {
            height: 200px;
        }

        .inputW {
            width: 150px;
        }

        .itemW {
            width: 200px;
        }

        .formLabelWidth {
            width: 100px;
        }

        .el-tabs--border-card > .el-tabs__content {
            padding: 0px;
        }
    </style>
</head>
<body>
<div class="page" id="page-monitor">
    <div class="page-title">员工奖励台账>员工奖励台账
        <!--        <el-button @click="exportData" type="primary" style="float: right">导出</el-button>-->
    </div>
    <div class="page-main">
        <div class="main-head">
            <el-form :inline="true" size="small">
                <el-form-item label="人员名称">
                    <el-input placeholder="请输入" class="inputW" v-model="user.userName"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-date-picker v-model="month" type="month" placeholder="选择月" style="width: 120px;">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="addBtn">设置奖惩</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="exportData">导出</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table :data="userData" height="660" :stripe="true" :border="true" size="mini" style="width: 100%">
            <el-table-column type="index" label="序号" width="50" align="center">
            </el-table-column>
            <el-table-column prop="userName" label="人员名称">
            </el-table-column>
            <el-table-column prop="total" label="任务总数"></el-table-column>
            <el-table-column prop="finished" label="任务完成数"></el-table-column>
            <el-table-column label="任务完成奖励">
                <template slot-scope="scope">
                    <el-tag size="medium">
                        {{scope.row.finished>0?totaskReward(scope.row.finished,scope.row.total):0}}元
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="found" label="发现隐患数"></el-table-column>
            <el-table-column label="发现隐患数奖励">
                <template slot-scope="scope">
                    <el-tag size="medium">{{scope.row.found>0?tofindRiskReward(scope.row.found):0}}元</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="processed" label="已处理隐患数"></el-table-column>
            <el-table-column label="已处理隐患数奖励">
                <template slot-scope="scope">
                    <el-tag size="medium">{{scope.row.processed>0?tohandleRiskReward(scope.row.processed):0}}元</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="unprocessed_timeout" label="超期未处理数"></el-table-column>
            <el-table-column label="超期未处理数惩罚">
                <template slot-scope="scope">
                    <el-tag size="danger">
                        {{scope.row.unprocessed_timeout>0?tooverdue(scope.row.finished,scope.row.unprocessed_timeout):0}}元
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="总奖惩">
                <template slot-scope="scope">
                    <el-tag v-bind:size="toNum(scope.row)>0?'medium':'danger'">
                        {{toNum(scope.row)}}元
                    </el-tag>
                </template>
            </el-table-column>
        </el-table>
        <div class="">
            <el-pagination background layout="prev, pager, next" :pagesize="user.pagesize"
                           :total="user.totalCount"
                           :current-page.sync="user.pageno" @current-change="UserChangePage">
            </el-pagination>
        </div>
    </div>

    <el-dialog :title="editMod?'编辑奖惩':'设置奖惩'" :visible.sync="dialogFormVisible">
        <el-form :model="param" ref="formName" :inline="true" style="margin-left: 55px">
            <el-form-item>
                <el-form-item label="任务完成奖励(元/100%)" :label-width="130" :rules="[
                             {required:true,message:'任务完成奖励!',trigger:'blur'}
                            ]">
                    <el-input v-model="param.taskReward" autocomplete="off" class="itemW"
                              prop="taskReward" type="number"></el-input>
                </el-form-item>
            </el-form-item>
            <el-form-item>
                <el-form-item label="发现隐患奖励(元/个)" :label-width="130" :rules="[
                             {required:true,message:'任务完成奖励!',trigger:'blur'}
                            ]">
                    <el-input v-model="param.findRiskReward" autocomplete="off" class="itemW"
                              prop="findRiskReward" type="number"></el-input>
                </el-form-item>
            </el-form-item>
            <el-form-item>
                <el-form-item label="处理隐患奖励(元/个)" :label-width="130" :rules="[
                             {required:true,message:'任务完成奖励!',trigger:'blur'}
                            ]">
                    <el-input v-model="param.handleRiskReward" autocomplete="off" class="itemW"
                              prop="handleRiskReward" type="number"></el-input>
                </el-form-item>
            </el-form-item>
            <el-form-item>
                <el-form-item label="超期未处理惩罚(元/个)" :label-width="130" :rules="[
                             {required:true,message:'任务完成奖励!',trigger:'blur'}
                            ]">
                    <el-input v-model="param.overdue" autocomplete="off" class="itemW"
                              prop="overdue" type="number"></el-input>
                </el-form-item>
            </el-form-item>


        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitClick">确 定</el-button>
        </div>
    </el-dialog>
</div>
<script type='text/javascript' src='../../../assets/bundle.js' charset='utf-8'></script>
<script type='text/javascript' src='../../../assets/jquery-1.11.2.min.js' charset='utf-8'></script>
<script type='text/javascript' src='../../../assets/echarts.min.js' charset='utf-8'></script>
<script type='text/javascript' src='../../../js/common.js' charset='utf-8'></script>
<script type='text/javascript' src='../../../js/component.js' charset='utf-8'></script>
<script>
    var vuePage = new Vue({
        el: '#page-monitor',
        data: {
            month: '',
            currentPage: 1,
            tableData: [],
            pagesize: 10,
            pageno: 1,
            totalCount: 0,
            tableData: [],
            departData: [],
            zoneData: [],
            projectNo: '',
            riskName: '',
            editMod: false,
            userData: [],
            user: {
                wf_tasktime: '',
                pageno: 1,
                pagesize: 10,
                totalCount: 0,
                userName: '',
            },
            param: {
                Id: '',
                projectNo: '',
                taskReward: '',
                findRiskReward: '',
                handleRiskReward: '',
                overdue: '',
            },
            editMod: false,
            dialogFormVisible: false,
        },
        created: function () {
            $('.page').show();
            this.month = TimeUtil.convertToString(new Date()).substr(0, 7);
            this.getuserData();
            this.loadReward();
        },
        methods: {
            exportData() {
                HttpUtil.get('RisktaskReward/ExportReward', {wf_tasktime: this.ToDate()}).success((res) => {
                    this.$message.success('导出成功');
                    window.location = SiteConfig.webApiset + res.data;
                })
            },
            totaskReward(finished, total) {
                return parseInt(((finished / total) * this.param.taskReward).toFixed(0));
            },
            tofindRiskReward(found) {
                return parseInt(found * this.param.findRiskReward);
            },
            tohandleRiskReward(processed) {
                return parseInt(processed * this.param.handleRiskReward);
            },
            tooverdue(finished, unprocessed_timeout) {
                // return parseInt(unprocessed_timeout * this.param.overdue);
                return parseInt((finished > 0 ? (unprocessed_timeout / finished) * this.param.overdue : unprocessed_timeout * this.param.overdue).toFixed(0));
            },
            toNum(row) {
                // (scope.row.finished>0?((scope.row.finished/scope.row.total)*20).toFixed(0):0)
                // +(scope.row.found>0?scope.row.found*5:0)
                return parseInt(row.finished > 0 ? ((row.finished / row.total) * this.param.taskReward).toFixed(0) : 0)
                    + parseInt(row.found > 0 ? row.found * this.param.findRiskReward : 0)
                    + parseInt(row.processed > 0 ? row.processed * this.param.handleRiskReward : 0)
                    - parseInt(row.finished > 0 ? ((row.unprocessed_timeout / row.finished) * this.param.overdue).toFixed(0) : row.unprocessed_timeout * this.param.overdue)

                // parseInt(row.unprocessed_timeout > 0 ? row.unprocessed_timeout * this.param.overdue : 0);
            },
            addBtn: function () {
                vuePage.dialogFormVisible = true;
            },
            loadReward() {
                var projectNo = '5668b0a17d2c4a52a2114b465db80357';
                // SiteConfig.account.projectNo
                HttpUtil.get('RisktaskReward/List', {projectNo: SiteConfig.account.projectNo}).success((r) => {
                    if (r.state == 0) {
                        if (r.data[0].projectNo) {
                            vuePage.param = r.data[0];
                            this.editMod = true;
                        } else {
                            this.editMod = false;
                            this.param = {};
                        }
                    }
                })
            },
            submitClick() {
                if (!this.param.taskReward) {
                    this.$message({type: 'error', message: '任务完成奖励不能为空'})
                    return;
                }
                if (!this.param.findRiskReward) {
                    this.$message({type: 'error', message: '发现隐患奖励不能为空'})
                    return;
                }
                if (!this.param.handleRiskReward) {
                    this.$message({type: 'error', message: '处理隐患奖励不能为空'})
                    return;
                }

                if (!this.param.overdue) {
                    this.$message({type: 'error', message: '超期未处理奖励不能为空'})
                    return;
                }

                if (vuePage.editMod) {
                    HttpUtil.get('RisktaskReward/Update', this.param).success((r) => {
                        if (r.state == 0) {
                            this.$message({
                                message: '修改成功！',
                                type: 'success'
                            });
                            vuePage.getuserData();
                            vuePage.loadReward();
                            setTimeout(function () {
                                vuePage.dialogFormVisible = false;
                            }, 1000)

                        } else {
                            vuePage.$message({type: 'error', message: '修改失败'})
                        }
                    })
                } else {
                    var projectNo = '5668b0a17d2c4a52a2114b465db80357';
                    // SiteConfig.account.projectNo
                    this.param.projectNo = SiteConfig.account.projectNo;
                    HttpUtil.get('RisktaskReward/Add', this.param).success((r) => {
                        if (r.state == 0) {

                            this.$message({
                                message: '添加成功！',
                                type: 'success'
                            });
                            vuePage.getuserData();
                            vuePage.loadReward();
                            setTimeout(function () {
                                vuePage.dialogFormVisible = false;
                            }, 1000)
                        } else {
                            vuePage.$message({type: 'error', message: '添加失败'})
                        }
                    })
                }
            },

            editClick(row) {
                this.param = row;
                this.dialogFormVisible = true;
                this.editMod = true;
            },
            p(s) {
                return s < 10 ? '0' + s : s
            },
            ToDate() {
                const d = new Date(this.month);
                const resDate = d.getFullYear() + '-' + this.p((d.getMonth() + 1)) + '-' + this.p(d.getDate());
                const resTime = this.p(d.getHours()) + ':' + this.p(d.getMinutes()) + ':' + this.p(d.getSeconds());
                return resDate;
            },
            UserChangePage(page) {
                this.user.pageno = page;
                this.getuserData();
            },
            search: function () {
                this.getuserData();
            },
            getuserData: function () {
                this.user.wf_tasktime = this.ToDate();
                HttpUtil.get('Risk/riskSummaryByUser', this.user).success((res) => {
                    var row = res.data;
                    for (var i = 0; i < row.length; i++) {
                        if (row[i].userName == SiteConfig.account.projectName) {
                            row.splice(i, 1);
                        }
                    }
                    this.userData = row;
                    this.user.totalCount = res.total;
                })
            },
        },
        mounted: function () {

        }
    });
</script>
</body>
</html>
